package qlttn.web.bean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import qlttn.business.EditorExamQuestionBO;
import qlttn.business.EditorExamQuestionBOImpl;
import qlttn.common.utility.RepeatPaginator;
import qlttn.common.valueobjects.ExamQuestionVO;
import qlttn.common.valueobjects.QuestionVO;
import qlttn.model.Choice;
import qlttn.model.ContentExam;
import qlttn.model.ContentExamId;
import qlttn.model.Course;
import qlttn.model.ExamQuestion;
import qlttn.model.Images;
import qlttn.model.MultiChoiceQuestion;
import qlttn.model.Question;
import qlttn.model.TrueFalseQuestion;
import qlttn.model.Users;
@ManagedBean
@SessionScoped
public class CreateExamQuestionBean{
	private String course;
	private String level;
	private String kindOfQuestion;
	private String groupQuestion;
	private List<QuestionVO> listQuestion;
	private static List<QuestionVO> listAddQuestion = new ArrayList<QuestionVO>();
	private static List<QuestionVO> listTempQuestion = new ArrayList<QuestionVO>();
	private EditorExamQuestionBO editorExamQuestionBO;
	private int order = 1;
	private int id = 1;
	private static int countAdd;
	private static int countRemove;
	private int isInitial;
	private RepeatPaginator paginator;
	private boolean mixChoice;
	private boolean mixQuestion;
	private ExamQuestionVO examQuestionVO;
	public CreateExamQuestionBean(){
		editorExamQuestionBO = new EditorExamQuestionBOImpl();
		listQuestion = new ArrayList<QuestionVO>();
			isInitial++;
		examQuestionVO = new ExamQuestionVO();
	}
	
	public String getCourse() {
		return course;
	}
	public void setCourse(String course) {
		this.course = course;
		System.out.println(course);
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
		System.out.println(level);
	}
	public String getKindOfQuestion() {
		return kindOfQuestion;
	}
	public void setKindOfQuestion(String kindOfQuestion) {
		this.kindOfQuestion = kindOfQuestion;
		System.out.println(kindOfQuestion);
	}
	
	
	/**
	 * @return the order
	 */
	public int getOrder() {
		if(isInitial == 1)
			order++;
		
		return order;
	}

	/**
	 * @param order the order to set
	 */
	public void setOrder(int order) {
		this.order = order;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id++;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the groupQuestion
	 */
	public String getGroupQuestion() {
		return groupQuestion;
	}

	/**
	 * @param groupQuestion the groupQuestion to set
	 */
	public void setGroupQuestion(String groupQuestion) {
		this.groupQuestion = groupQuestion;
		System.out.println(groupQuestion);
	}

	/**
	 * @return the listQuestion
	 */
	public List<QuestionVO> getListQuestion() {
		return listQuestion;
	}

	/**
	 * @param listQuestion the listQuestion to set
	 */
	public void setListQuestion(List<QuestionVO> listQuestion) {
		this.listQuestion = listQuestion;
	}

	
	/**
	 * @return the listAddQuestion
	 */
	public List<QuestionVO> getListAddQuestion() {
		return listAddQuestion;
	}

	/**
	 * @param listAddQuestion the listAddQuestion to set
	 */
	public void setListAddQuestion(List<QuestionVO> listAddQuestions) {
		listAddQuestion = listAddQuestions;
	}
	
	public RepeatPaginator getPaginator() {
		return paginator;
	}

	public void setPaginator(RepeatPaginator paginator) {
		this.paginator = paginator;
	}


	/**
	 * @return the mixQuestion
	 */
	public boolean isMixQuestion() {
		return mixQuestion;
	}

	/**
	 * @param mixQuestion the mixQuestion to set
	 */
	public void setMixQuestion(boolean mixQuestion) {
		this.mixQuestion = mixQuestion;
	}

	
	
	/**
	 * @return the examQuestionVO
	 */
	public ExamQuestionVO getExamQuestionVO() {
		return examQuestionVO;
	}

	/**
	 * @param examQuestionVO the examQuestionVO to set
	 */
	public void setExamQuestionVO(ExamQuestionVO examQuestionVO) {
		this.examQuestionVO = examQuestionVO;
	}

	/**
	 * @return the mixChoice
	 */
	public boolean isMixChoice() {
		return mixChoice;
	}

	/**
	 * @param mixChoice the mixChoice to set
	 */
	public void setMixChoice(boolean mixChoice) {
		this.mixChoice = mixChoice;
	}
	public List<SelectItem> getListCourse(){
		List<SelectItem> listCourse = new ArrayList<SelectItem>();
		listCourse.add(new SelectItem("unknown", "Course"));
		listCourse.add(new SelectItem("1", "Toán"));
		listCourse.add(new SelectItem("2","Lý"));
		listCourse.add(new SelectItem("3","Hóa"));
		listCourse.add(new SelectItem("4","Anh Văn"));
		
		return (listCourse);
	}
	public List<SelectItem> getListQuestionGroup(){
		List<SelectItem> listQuestionGroup = new ArrayList<SelectItem>();
		listQuestionGroup.add(new SelectItem("unknown", "Group Question"));
		listQuestionGroup.add(new SelectItem("1", "Đại Số"));
		listQuestionGroup.add(new SelectItem("1","Hình Học"));
		listQuestionGroup.add(new SelectItem("2","Cơ học"));
		listQuestionGroup.add(new SelectItem("2","Động Lực "));
		listQuestionGroup.add(new SelectItem("3","Hữu Cơ"));
		listQuestionGroup.add(new SelectItem("3","Vô Cơ"));
		return (listQuestionGroup);
	}
	public SelectItem[] getListLevel() {
		SelectItem[] listLevel = { new SelectItem("unknown", "Choice level"),
				new SelectItem("Easy", "Easy"),
				new SelectItem("Midium", "Midium"),
				new SelectItem("Hard", "Hard") };
		
		return listLevel;
	}
	public List<SelectItem> getListKindQuestion(){
		List<SelectItem> listKindQuestion = new ArrayList<SelectItem>();
		listKindQuestion.add(new SelectItem("unknown", "KindOfQuestion"));
		listKindQuestion.add(new SelectItem("1", "Multi choice"));
		listKindQuestion.add(new SelectItem("2","Fill Text"));
		listKindQuestion.add(new SelectItem("3","Yes/No"));
		
		return (listKindQuestion);
	}
	

	public void loadQuestion() {
		System.out.println("Listenning load.....!");
		int courseId = 0;
		int groupId = 0;
		try{
			courseId = Integer.parseInt(course);
		}catch(NumberFormatException ex){
			courseId = 0;
		}
		try{
			groupId = Integer.parseInt(groupQuestion);
		}catch(NumberFormatException ex){
			groupId = 0;
		}
		if(level.equals("unknown")) level = "";
		if(kindOfQuestion.equals("unknown")) kindOfQuestion = "";
		this.listQuestion = editorExamQuestionBO.loadQuestion(courseId, groupId, kindOfQuestion, level);
		System.out.println("----------ok---"+this.level+this.course+this.groupQuestion+this.kindOfQuestion+this.listQuestion);
		
		//-----------------------------------------------------
		for (int i = 0; i < listQuestion.size(); i++) {
			// System.out.println("Vao` for đây");
			String kind = listQuestion.get(i).getKindOfQuestion();
			
			// load image
			String image_name_path = "../Image_q/";
			Images image = editorExamQuestionBO.loadImage(listQuestion.get(i).getQuestionID());
			String name = image.getName();
			if (name != null) {
				image_name_path += name;
				listQuestion.get(i).setImageName(image_name_path);
			}
			if (kind.equalsIgnoreCase("MultiChoice")) {
				List<SelectItem> listChoiceSelectItem = new ArrayList<SelectItem>();
				System.out.println("Vao` if đây" + i);
				MultiChoiceQuestion mutilQuestion = editorExamQuestionBO.loadMultiQuestion(listQuestion.get(i).getQuestionID());
				// load cau hoi multi

				List<Choice> liseChoice = new ArrayList<Choice>();
				liseChoice = editorExamQuestionBO.loadListChoice(mutilQuestion
						.getMultiId());
				String[] dapanTitle= {"A","B","C","D","E","F","G","H","I","K"};
				for (int j = 0; j < liseChoice.size(); j++) {
					System.out.println("xem id choice:"+liseChoice.get(j).getChoiceId());
					listChoiceSelectItem.add(new SelectItem(liseChoice.get(j).getChoiceId(),"   "+dapanTitle[j]+".   "+ liseChoice.get(j).getContent()));
				}
				//
				listQuestion.get(i).setListChoice_VO(liseChoice);
				listQuestion.get(i).setListSelectChoice(listChoiceSelectItem);
				listQuestion.get(i).setMultiChoice(mutilQuestion.getIsMutilChoice());
				// cau hoi multi co nhiu lua chon

			} else if(kind.equalsIgnoreCase("TrueFalseQuestion")){ // cau hoi true false
				System.out.println("vao truefalse" + i);
				TrueFalseQuestion trueFalseQuestion = editorExamQuestionBO.loadTrueFalseQuestion(listQuestion.get(i).getQuestionID());
				listQuestion.get(i).setTrue1Question(trueFalseQuestion.getResult());
			}
		}
		
		setPaginator(new RepeatPaginator(listQuestion));
	}
	
	public void addQuestion(){
		countRemove = 0;
		int size = this.listQuestion.size();
		for(int index = 0; index < size; index++){
			QuestionVO question = this.listQuestion.get(index);
			if(question.isCheckStatus()==true){
				if(!this.checkContains(listAddQuestion, question)){
					question.setCheckStatus(false);
					question.setOrder(++countAdd);
					listAddQuestion.add(question);
					listTempQuestion.add(question);
					this.listQuestion.remove(index);
					size--;index--;
				}
			}else{
				question.setOrder(++countRemove);
			}
		}
		
			
		setPaginator(new RepeatPaginator(listAddQuestion));
	}
	
	public boolean checkContains(List<QuestionVO> listQuestionVO, QuestionVO questionVO){
		boolean result = false;
		for(QuestionVO q:listQuestionVO){
			if(q.getQuestionID()==questionVO.getQuestionID()){
				result = true;
				break;
			}
		}
		
		return result;
	}

	public void removeQuestion(){
		countAdd = 0;
		int size = listAddQuestion.size();
		for(int index = 0; index < size; index++){
			QuestionVO question = listAddQuestion.get(index);
			if(question.isCheckStatus()==true){
				question.setOrder(++countRemove);
				question.setCheckStatus(false);
				this.listQuestion.add(question);
				listAddQuestion.remove(index);
				listTempQuestion.remove(question);
				size--;index--;
			}else{
				question.setOrder(++countAdd);
			}
		}
		setPaginator(new RepeatPaginator(listAddQuestion));
	}

	public static List randomList(List listVO) {
		List newListVO= new ArrayList();
		List<Integer> listInt = new ArrayList<Integer>();
		Random rd = new Random();
		for (int i = 0; i < listVO.size(); i++) {
			int random;
			for (; ;) {
				random=rd.nextInt(listVO.size());
				if(!listInt.contains(random)){
					break;
				}
			}
			
			listInt.add(random);
			newListVO.add(listVO.get(random));
		}
		return newListVO;
	}
	
	public void mixChoices(){
		if(this.mixChoice == true){
			for (int i = 0; i < listAddQuestion.size(); i++) {
				// System.out.println("Vao` for đây");
				String kind = listAddQuestion.get(i).getKindOfQuestion();
				
				if (kind.equalsIgnoreCase("MultiChoice")) {
					List<SelectItem> listChoiceSelectItem = new ArrayList<SelectItem>();
					
					// load cau hoi multi
					List<Choice> liseChoice = new ArrayList<Choice>();
					liseChoice = randomList(listAddQuestion.get(i).getListChoice_VO());
					String[] dapanTitle= {"A","B","C","D","E","F","G","H","I","K"};
					for (int j = 0; j < liseChoice.size(); j++) {
						listChoiceSelectItem.add(new SelectItem(liseChoice.get(j).getChoiceId(),"   "+dapanTitle[j]+".   "+ liseChoice.get(j).getContent()));
					}
					//
					listAddQuestion.get(i).setListChoice_VO(liseChoice);
					listAddQuestion.get(i).setListSelectChoice(listChoiceSelectItem);
				} 
			}
		}else{
			for (int i = 0; i < listAddQuestion.size(); i++) {
				String kind = listAddQuestion.get(i).getKindOfQuestion();
				
				if (kind.equalsIgnoreCase("MultiChoice")) {
					List<SelectItem> listChoiceSelectItem = new ArrayList<SelectItem>();
					
					// load cau hoi multi
					List<Choice> listChoice = new ArrayList<Choice>();
					listChoice = listTempQuestion.get(i).getListChoice_VO();
					String[] dapanTitle= {"A","B","C","D","E","F","G","H","I","K"};
					for (int j = 0; j < listChoice.size(); j++) {
						System.out.println(listChoice.get(j).getContent());
						listChoiceSelectItem.add(new SelectItem(listChoice.get(j).getChoiceId(),"   "+dapanTitle[j]+".   "+ listChoice.get(j).getContent()));
					}
					//
					System.out.println(listChoice.toString());
					listAddQuestion.get(i).setListChoice_VO(listChoice);
					listAddQuestion.get(i).setListSelectChoice(listChoiceSelectItem);
				} 
			}
		}		
	}
	
	public void mixQuestions(){
		if(this.mixQuestion == true){
			System.out.println("Mix Question");
			listAddQuestion = randomList(listAddQuestion);
			for (int i = 0; i < listAddQuestion.size(); i++) {
				// System.out.println("Vao` for đây");
				String kind = listAddQuestion.get(i).getKindOfQuestion();
				
				if (kind.equalsIgnoreCase("MultiChoice")) {
					List<SelectItem> listChoiceSelectItem = new ArrayList<SelectItem>();
					
					// load cau hoi multi
					List<Choice> liseChoice = new ArrayList<Choice>();
					liseChoice = randomList(listAddQuestion.get(i).getListChoice_VO());
					String[] dapanTitle= {"A","B","C","D","E","F","G","H","I","K"};
					for (int j = 0; j < liseChoice.size(); j++) {
						listChoiceSelectItem.add(new SelectItem(liseChoice.get(j).getChoiceId(),"   "+dapanTitle[j]+".   "+ liseChoice.get(j).getContent()));
					}
					//
					listAddQuestion.get(i).setListChoice_VO(liseChoice);
					listAddQuestion.get(i).setListSelectChoice(listChoiceSelectItem);
				} 
			}
			
		}else{
			listAddQuestion = listTempQuestion;
			for (int i = 0; i < listAddQuestion.size(); i++) {
				// System.out.println("Vao` for đây");
				String kind = listAddQuestion.get(i).getKindOfQuestion();
				
				if (kind.equalsIgnoreCase("MultiChoice")) {
					List<SelectItem> listChoiceSelectItem = new ArrayList<SelectItem>();
					
					// load cau hoi multi
					List<Choice> liseChoice = new ArrayList<Choice>();
					liseChoice = randomList(listAddQuestion.get(i).getListChoice_VO());
					String[] dapanTitle= {"A","B","C","D","E","F","G","H","I","K"};
					for (int j = 0; j < liseChoice.size(); j++) {
						listChoiceSelectItem.add(new SelectItem(liseChoice.get(j).getChoiceId(),"   "+dapanTitle[j]+".   "+ liseChoice.get(j).getContent()));
					}
					//
					listAddQuestion.get(i).setListChoice_VO(liseChoice);
					listAddQuestion.get(i).setListSelectChoice(listChoiceSelectItem);
				} 
			}
		}		
	}

	public String saveExamQuestion(){
		String forward = "createEQSuccess";
		//Create Exam Question Success...!
		ExamQuestion examQuestion = new ExamQuestion();
		Set<ContentExam> setContentExams = new HashSet<ContentExam>();
		for(QuestionVO questionVO:listAddQuestion){
			// Create Question
			Question question = new Question();
			question.setQuestionId(questionVO.getQuestionID());
			
			//Create ContentExamId
			ContentExamId contentExamId = new ContentExamId();
			contentExamId.setExamQid(examQuestion.getExamQid());
			contentExamId.setQuestionId(questionVO.getQuestionID());
			contentExamId.setScore(questionVO.getScore());
			
			//Create ContentExam
			ContentExam contentExam = new ContentExam();
			contentExam.setExamQuestion(examQuestion);
			contentExam.setQuestion(question);
			contentExam.setId(contentExamId);
			
			//Add ContentExam to HashSet
			setContentExams.add(contentExam);
		}
		
		// Create Users
		Users users = new Users();
		users.setUserId(6);
		//Course of Exam Question.
		Course course = new Course();
		try{
			int courseID = Integer.parseInt(this.course);
			course.setCourseId(courseID);
		}catch(NumberFormatException e){
			
		}
		//Set attributes to ExamQuestion object.
		examQuestion.setName(this.examQuestionVO.getName());
		examQuestion.setDuration(this.examQuestionVO.getTimeLine());
		examQuestion.setDescription(this.examQuestionVO.getDescription());
		examQuestion.setIsActive(this.examQuestionVO.isActive());
		examQuestion.setUsers(users);
		examQuestion.setContentExams(setContentExams);
		examQuestion.setCourse(course);
		
		boolean result = editorExamQuestionBO.saveExamQuestion(examQuestion);
		System.out.println(result);
		return forward;
	}
	
}
